/**
 * Created by 白雨浓 on 2018/1/16 11:38.
 */
import {types} from './types';
import electron from 'electron';
import path from 'path';
import fs from 'fs';
import ejs from 'ejs';

export default {
    entity(self, table, fields){

        const dataPath = (electron.app || electron.remote.app).getPath('userData');
        const filePath = path.join(dataPath, 'entity.ejs');
        if (!fs.existsSync(filePath)) {
            self.$$msg.error('entity模板文件缺失');
            return;
        }
        const tmpFile = fs.readFileSync(filePath, 'utf-8');

        let clsName = self.$str.toCamel(table);
        clsName = clsName.substring(0, 1).toUpperCase() + clsName.substring(1);

        let tmp = {
            packageName: self.config.packageName,
            author: self.config.author,
            date: self.$date.format(new Date(), 'yyyy/MM/dd hh:mm'),
            clsName: clsName,
            variables: []
        };

        for (const field of fields) {
            if (field.COLUMN_NAME === 'id') {
                tmp.variables.push({type: 'int', name: 'id', comment: '自增id'});
            } else if (field.COLUMN_NAME === 'state') {
                tmp.variables.push({type: 'Boolean', name: 'state', comment: '状态'});
            } else {
                tmp.variables.push({
                    type: types[field.DATA_TYPE],
                    name: self.$str.toCamel(field.COLUMN_NAME),
                    comment: field.COLUMN_COMMENT
                });
            }
        }

        return ejs.render(tmpFile, tmp);
    },
};
